﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Runtime.Session;
using SingleUserMall.Adverts.VO;
using SingleUserMall.Authorization.Users;
using SingleUserMall.Common;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace SingleUserMall.Adverts
{
    [AbpAuthorize]
    public class AdvertAppService : SingleUserMallAppServiceBase, IAdvertAppService
    {
        private readonly IRepository<Advert, long> _advertRepository;
        private readonly IRepository<AdvertRecord, long> _advertRecordRepository;
        private readonly IRepository<UserRedPacketDetail, long> _userRedPacketDetailRepository;
        private readonly IRepository<User, long> _userRepository;

        public AdvertAppService(
            IRepository<Advert, long> advertRepository,
            IRepository<AdvertRecord, long> advertRecordRepository,
            IRepository<UserRedPacketDetail, long> userRedPacketDetailRepository,
            IRepository<User, long> userRepository
            )
        {
            _advertRepository = advertRepository;
            _advertRecordRepository = advertRecordRepository;
            _userRedPacketDetailRepository = userRedPacketDetailRepository;
            _userRepository = userRepository;
        }

        public async Task<PagedResultDto<AdvertAppVO>> GetAdvertsAsync(PagedForm form)
        {
            var userId = AbpSession.GetUserId();
            var user = _userRepository.FirstOrDefault(x => x.Id == userId);
            var query = _advertRepository.GetAll()
                .Where(x => x.AdvertSex == user.UserSex || x.AdvertSex == "全部")
                .Where(x => x.AdvertAge == user.UserAge || x.AdvertAge == "全部")
                .Where(x => x.AdvertIncome == user.UserIncome || x.AdvertIncome == User.UserIncomeStatus.All)
                .Where(x=> x.IsUse)
                .OrderByDescending(x=> x.CreationTime).ThenByDescending(x=> x.WatchTimes);
            return await query.ToPagedResultDtoAsync<Advert, AdvertAppVO>(form, (i, vo) =>
            {

            });
        }

        public async Task<decimal> AfterWatchingAdvert(SingleId<long> form)
        {
            var userId = AbpSession.GetUserId();
            var user = _userRepository.FirstOrDefault(x => x.Id == userId);
            var advertRecord = _advertRecordRepository.FirstOrDefault(x => x.UserId == userId && x.AdvertId == form.Id);
            var advter  = _advertRepository.FirstOrDefault(form.Id);
            advter.WatchTimes++;
            if(advertRecord == null)
            {
                _advertRecordRepository.Insert(new AdvertRecord { UserId = userId, AdvertId = form.Id, WatchDateTime = DateTime.Now });
                _userRedPacketDetailRepository.Insert(new UserRedPacketDetail { ExchangeTime = DateTime.Now, UserId = userId, ExchangeAmount = advter.RedPacketAmount, ExchangeReason = $"观看视频赠送{advter.RedPacketAmount}元红包" });
                user.UserRedPacket += advter.RedPacketAmount;
                return await Task.FromResult(advter.RedPacketAmount);
            }
            else
            {
                advertRecord.WatchDateTime = DateTime.Now;
                return await Task.FromResult(0);
            }
        }
    }
}
